package controller.ur;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import model.dao.CommentDaoLocal;
import model.entity.Comment;
import controller.CUtil;

/**
 * Servlet implementation class SPhotoRemoveComment
 */
public class SPhotoRemoveComment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	@EJB
	private CommentDaoLocal commentDao;
    
	private Logger log=Logger.getLogger(SPhotoRemoveComment.class);
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public SPhotoRemoveComment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(!CUtil.checkLogIn(request, response))
			return;
		
		int commentId=Integer.parseInt((String)request.getParameter("commentId"));
		int photoId=Integer.parseInt((String)request.getParameter("photoId"));
		Comment comment=commentDao.findById(commentId);
		log.info("Comment removed: "+comment.toString());
		commentDao.remove(comment);
		CUtil.redirecter(request, response, "SViewPhoto?photoId="+photoId);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
